package com.NewsServlet;

import com.NewsService.NewsService;
import com.NewsServiceImpl.NewsServiceImpl;
import com.alibaba.fastjson.JSON;
import com.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name= req.getParameter("username");
        String pass= req.getParameter("password");
        NewsService service = new NewsServiceImpl();
        User user = service.getSeleUser(name,pass);
        String mag="";
        if(user!=null){
            mag="登录成功！";
        }else {
            mag="登录失败！";
        }
        //resp.setCharacterEncoding("text/html;charset=utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        Map map = new HashMap();
        map.put("name","lisi");
        map.put("name","lisi");

        //String json1= "{\"id\":0,\"name\":\"admin\",\"pass\":\"admin\"}";
        String json=  JSON.toJSONString(map);
        System.out.println("---->"+json);
        writer.print(json);
        writer.flush();
        writer.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         String name=   req.getParameter("name");
         String pass=  req.getParameter("password");
         //访问数据库查询，调用service接口完成查询
         User user = new User();
         HttpSession session= req.getSession();
         session.setAttribute("user",user);
         resp.sendRedirect(req.getContextPath() + "/index.jsp");

    }
}
